package com.loopj.android.http.util;

import org.apache.http.Header;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

/**
 * 网络请求逻辑层
 * 
 * @author xindaoapp
 * 
 */
public class XindaoRestClient {
	private static AsyncHttpClient client = new AsyncHttpClient();

	public XindaoRestClient() {
		super();
	}

	public void get(final MoccaRequest moccaRequest) {
		client.get(moccaRequest.url, moccaRequest.params, new AsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
				moccaRequest.parse(statusCode, headers, responseBody);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
				moccaRequest.errorListener.onErrorResponse(null);
			}
		});
	}

	public void post(final MoccaRequest moccaRequest) {
		client.post(null, moccaRequest.url, moccaRequest.getHeaders(), moccaRequest.params, null,
				new AsyncHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
						moccaRequest.parse(statusCode, headers, responseBody);
					}

					@Override
					public void onFailure(int statusCode, Header[] headers, byte[] responseBody,
							Throwable error) {
						moccaRequest.errorListener.onErrorResponse(null);
					}
				});
	}

}
